package com.xiguaji.tiktok.v1.web;

import com.xiguaji.tiktok.utils.CheckMobile;
import com.xiguaji.tiktok.v1.tiktok.permission.repository.UserRoleRepository;
import com.xiguaji.tiktok.v1.user.dto.UserReturnDto;
import com.xiguaji.tiktok.v1.user.model.User;
import com.xiguaji.tiktok.v1.user.service.UserService;
import com.xiguaji.tiktok.v1.user.validate.UserHandler;
import com.xiguaji.tiktok.v1.web.utils.FormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.ModelAndView;

public class BaseController extends UserHandler {
    @Value("${server.domain}")
    protected String serverDomain;

    @Autowired
    UserRoleRepository userRoleRepository;

    @Autowired
    UserService userService;

    protected ModelAndView getModel(int tab){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("isMobile", CheckMobile.check());
        modelAndView.addObject("tab",tab);
        modelAndView.addObject("contextPath",serverDomain);
        //用户信息
        UserReturnDto user = getUserReturn();
        modelAndView.addObject("login", user != null);
        modelAndView.addObject("user", user);
        modelAndView.addObject("myController",this);
        modelAndView.addObject("roleId",user != null ? getRoleId(): user);
        return modelAndView;
    }

    protected UserReturnDto getUserReturn(){
        //用户信息
        UserReturnDto user = null;
        User u = getUser();
        if(null != u){
            user = userService.getUser(u.getId());
        }
        return user;
    }

    protected ModelAndView getModel(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("isMobile", CheckMobile.check());
        modelAndView.addObject("contextPath",serverDomain);
        //用户信息
        UserReturnDto user = null;
        User u = getUser();
        if(null != u){
            user = userService.getUser(u.getId());
        }
        modelAndView.addObject("login", user != null);
        modelAndView.addObject("user", user);
        modelAndView.addObject("myController",this);
        modelAndView.addObject("roleId",user != null ? getRoleId(): user);
        return modelAndView;
    }

    public String getNumber(long num) {
        return FormatUtil.getNumber(num);
    }
    public String getNumber(double num) {
        return FormatUtil.getNumber(num);
    }
    public String getNumberForDouble(double num) {
        return FormatUtil.getNumber(num);
    }

}
